#include "UITheme.h"
#include "Control.h"
#include "Button.h"
#include "Widget.h"
#include "TextBox.h"
#include <Shlwapi.h>
#include "Progress.h"
void UITheme::Init()
{
	tex.LoadTexture("DefaultSkin.png");
	texSkinId = tex.texId;
	glEnable(GL_TEXTURE_2D);
	ttf = TrueTypeFont::GetInst();
}

void UITheme::Draw(Control* ctrl)
{
	glBindTexture(GL_TEXTURE_2D, texSkinId);
	//cout << ctrl->GetClassName().GetString().data() << endl;
	tstring tstrClss = ctrl->GetClassName();
	if (tstrClss.GetWString() == _T("Button"))
	{
		Button* btnCtrl = (Button*)ctrl;
		int z = btnCtrl->zIndex;
		int state = btnCtrl->bPressed ? 2 : btnCtrl->bHovered ? 1 : 0;
		int offY = 0;
		switch (state)
		{
		case 0:
			break;
		case 1:
			offY = 32.f;
			break;
		case 2:
			offY = 96.f;
			break;
		default:
			break;
		}
		ImageRect ir;
		ir.SetTexRect(480.0f, 511.f, offY, offY + 31.f);
		ir.SetVRect(ctrl->pos.x, ctrl->pos.x + ctrl->size.x, ctrl->pos.y, ctrl->pos.y + ctrl->size.y);
		ir.zIndex = ctrl->zIndex;
		ir.imageWidth = 512.f;
		ir.imageHeight = 512.f;
		DrawRectImage(ir);
	}
	else if (tstrClss.GetWString() == _T("Widget"))
	{
		int z = ctrl->zIndex;
		ImageRect ir;
		ir.SetTexRect(0.0f, 127.f, 0.f, 122.f);
		ir.SetVRect(ctrl->pos.x, ctrl->pos.x + ctrl->size.x, ctrl->pos.y, ctrl->pos.y + ctrl->size.y);
		ir.zIndex = ctrl->zIndex;
		ir.imageWidth = 512.f;
		ir.imageHeight = 512.f;
		DrawRectImage(ir);

	}
	else if (tstrClss.GetWString() == _T("ScrollableBar"))
	{
		int z = ctrl->zIndex;
		int offY = 0;

		/*glBindTexture(GL_TEXTURE_2D, texSkinId);
		glBegin(GL_QUADS);
		glTexCoord2f(480.0f / 512.0f, 0.f / 512.0f);
		glVertex3f(ctrl->pos.x, ctrl->pos.y, z);

		glTexCoord2f(510.0f / 512.0f, 0.f / 512.0f);
		glVertex3f(ctrl->pos.x + ctrl->size.x, ctrl->pos.y, z);

		glTexCoord2f(510.0f / 512.0f, 126.0f / 512.0f);
		glVertex3f(ctrl->pos.x + ctrl->size.x, ctrl->pos.y + ctrl->size.y, z);

		glTexCoord2f(480.0 / 512.0f, 126.0f / 512.0f);
		glVertex3f(ctrl->pos.x, ctrl->pos.y + ctrl->size.y, z);
		glEnd();*/
	}
	else if (tstrClss.GetWString() == _T("TextBox"))
	{
		TextBox* text = (TextBox*)ctrl;
		tstring tstr = text->content;
		wchar_t wch[2048];
		memset(wch, 0, 2048);
		wcscpy(wch, tstr.GetWString().data());
		ttf->drawText(wch, text->pos.x, text->pos.y, 200, 20);
	}
	else if (tstrClss.GetWString() == _T("Progress"))
	{
		Progress* p = (Progress*)ctrl;
		ImageRect ir;
		ir.SetVRect(ctrl->pos.x, ctrl->pos.x + ctrl->size.x, ctrl->pos.y, ctrl->pos.y + ctrl->size.y);
		ir.zIndex = ctrl->zIndex;
		ir.imageWidth = 512.f;
		ir.imageHeight = 512.f;
		if (p->IsOrientDirHorizen())
		{
			ir.SetTexRect(384.0f, 510.f, 144.f, 158.f);
		}
		else {
			//ir.SetTexRect(384.0f, 510.f, 144.f, 158.f);
			ir.SetTexRect(400.0f, 414.f, 208.f, 334.f);
		}
		DrawRectImage(ir);
		if (p->IsOrientDirHorizen())
		{
			ir.SetVRect(ctrl->pos.x, ctrl->pos.x + (ctrl->size.x*(p->GetValue()/(p->GetMaxValue()-p->GetMinValue()))), ctrl->pos.y, ctrl->pos.y + ctrl->size.y);
			ir.SetTexRect(384.0f, 510.f, 176.f, 190.f);
		}
		else {
			//ir.SetTexRect(384.0f, 510.f, 144.f, 158.f);
			ir.SetVRect(ctrl->pos.x, ctrl->pos.x + ctrl->size.x, ctrl->pos.y, ctrl->pos.y + ctrl->size.y*(p->GetValue() / (p->GetMaxValue() - p->GetMinValue())));
			ir.SetTexRect(432.0f, 446.f, 208.f, 334.f);
		}
		DrawRectImage(ir);
	}
}

void UITheme::DrawRectImage(ImageRect rect)
{
	glBegin(GL_QUADS);
	vec2 tex;
	tex = SetTexCoord(rect.texLeft, rect.texTop, rect.imageWidth, rect.imageHeight);
	//glVertex3f(v[0].x, v[0].y, v[0].z);
	glVertex3f(rect.vLeft, rect.vTop, rect.zIndex);
	tex = SetTexCoord(rect.texRight, rect.texTop, rect.imageWidth, rect.imageHeight);
	//glVertex3f(v[1].x, v[1].y, v[1].z);
	glVertex3f(rect.vRight, rect.vTop, rect.zIndex);

	tex = SetTexCoord(rect.texRight, rect.texBottom, rect.imageWidth, rect.imageHeight);
	//glVertex3f(v[2].x, v[2].y, v[2].z);
	glVertex3f(rect.vRight, rect.vBottom, rect.zIndex);

	tex = SetTexCoord(rect.texLeft, rect.texBottom, rect.imageWidth, rect.imageHeight);
	glVertex3f(rect.vLeft, rect.vBottom, rect.zIndex);
	//glVertex3f(v[3].x, v[3].y, v[3].z);
	//
	glEnd();
}

